|
1
|
|
|
import * as fs from "fs" |
|
2
|
|
|
import { WriteTags } from "../types/Tags" |
|
3
|
|
|
import { create } from "./create" |
|
4
|
|
|
import { removeTagsFromBuffer } from "./remove" |
|
5
|
|
|
import { isFunction, isString } from "../util" |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Callback signature for successful asynchronous update and write operations. |
|
9
|
|
|
* |
|
10
|
|
|
* @public |
|
11
|
|
|
*/ |
|
12
|
|
|
export type WriteSuccessCallback = |
|
13
|
|
|
(error: null, data: Buffer) => void |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Callback signature for failing asynchronous update and write operations. |
|
17
|
|
|
* |
|
18
|
|
|
* @public |
|
19
|
|
|
*/ |
|
20
|
|
|
export type WriteErrorCallback = |
|
21
|
|
|
(error: NodeJS.ErrnoException | Error, data: null) => void |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Callback signatures for asynchronous update and write operations. |
|
25
|
|
|
* |
|
26
|
|
|
* @public |
|
27
|
|
|
*/ |
|
28
|
|
|
export type WriteCallback = |
|
29
|
|
|
WriteSuccessCallback & WriteErrorCallback |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Replaces any existing tags with the given tags in the given buffer. |
|
33
|
|
|
* |
|
34
|
|
|
* @public |
|
35
|
|
|
*/ |
|
36
|
|
|
export function write(tags: WriteTags, buffer: Buffer): Buffer |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Replaces synchronously any existing tags with the given tags in the |
|
40
|
|
|
* specified file. |
|
41
|
|
|
* |
|
42
|
|
|
* @public |
|
43
|
|
|
*/ |
|
44
|
|
|
export function write(tags: WriteTags, filepath: string): true | Error |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Replaces asynchronously any existing tags with the given tags in the |
|
48
|
|
|
* given buffer or specified file. |
|
49
|
|
|
* |
|
50
|
|
|
* @public |
|
51
|
|
|
*/ |
|
52
|
|
|
export function write( |
|
53
|
|
|
tags: WriteTags, filebuffer: string | Buffer, callback: WriteCallback |
|
54
|
|
|
): void |
|
55
|
|
|
|
|
56
|
|
|
export function write( |
|
57
|
|
|
tags: WriteTags, |
|
58
|
|
|
filebuffer: string | Buffer, |
|
59
|
|
|
callback?: WriteCallback |
|
60
|
|
|
): Buffer | true | Error | void { |
|
61
|
|
|
const tagsBuffer = create(tags) |
|
62
|
|
|
|
|
63
|
|
|
if (isFunction(callback)) { |
|
64
|
|
|
if (isString(filebuffer)) { |
|
65
|
|
|
return writeAsync(tagsBuffer, filebuffer, callback) |
|
66
|
|
|
} |
|
67
|
|
|
return callback(null, writeInBuffer(tagsBuffer, filebuffer)) |
|
68
|
|
|
} |
|
69
|
|
|
if (isString(filebuffer)) { |
|
70
|
|
|
return writeSync(tagsBuffer, filebuffer) |
|
71
|
|
|
} |
|
72
|
|
|
return writeInBuffer(tagsBuffer, filebuffer) |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
function writeInBuffer(tags: Buffer, buffer: Buffer) { |
|
76
|
|
|
buffer = removeTagsFromBuffer(buffer) || buffer |
|
77
|
|
|
return Buffer.concat([tags, buffer]) |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
function writeAsync(tags: Buffer, filepath: string, callback: WriteCallback) { |
|
81
|
|
|
fs.readFile(filepath, (error, data) => { |
|
82
|
|
|
if (error) { |
|
83
|
|
|
callback(error, null) |
|
84
|
|
|
return |
|
85
|
|
|
} |
|
86
|
|
|
const newData = writeInBuffer(tags, data) |
|
87
|
|
|
fs.writeFile(filepath, newData, 'binary', (error) => { |
|
88
|
|
|
if (error) { |
|
89
|
|
|
callback(error, null) |
|
90
|
|
|
} else { |
|
91
|
|
|
callback(null, newData) |
|
92
|
|
|
} |
|
93
|
|
|
}) |
|
94
|
|
|
}) |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
function writeSync(tags: Buffer, filepath: string) { |
|
98
|
|
|
try { |
|
99
|
|
|
const data = fs.readFileSync(filepath) |
|
100
|
|
|
const newData = writeInBuffer(tags, data) |
|
101
|
|
|
fs.writeFileSync(filepath, newData, 'binary') |
|
102
|
|
|
return true |
|
103
|
|
|
} catch(error) { |
|
104
|
|
|
return error as Error |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|